/*
* $Id: Application.cpp 329 2011-08-11 08:39:16Z volokaleks@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Application.h"
#include "LanguageManager.h"
#include "Database/DatabaseManager.h"
#include "Visual/VisualManager.h"
#include "Settings/SettingsManager.h"
#include "Components/Components.h"
#include "Event/EventManager.h"

static IApplication* g_App = NULL;

IApplication* IApplication::GetApp(){
	if(!g_App)
		g_App = new CApplication;
	return g_App;
}

CApplication::CApplication(){}
CApplication::~CApplication(){
	for(std::vector<IComponent*>::iterator it = m_aComponents.begin(); it != m_aComponents.end(); it++)
		(*it)->Free();
}
BOOL CApplication::InitInstance(){
	//Create main components
	ILanguageManager::GetLanguageManager()->Init();
	IDatabaseManager::GetDatabaseManager()->Initialize();
	m_aComponents.push_back(new CSettingsComponent);
	m_aComponents.back()->Init();
	m_aComponents.push_back(new CJournalComponent);
	m_aComponents.back()->Init();
	ISettingsManager::GetSettingsManager()->Init();
	IVisualManager::GetVisualManager()->Init();
//Init common controls
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);
	CWinApp::InitInstance();
//Create main window
	m_pMainWnd = dynamic_cast<CWnd*>(IVisualManager::GetVisualManager()->GetMainWindow());
	WNDCLASS wndcls;
	wndcls.style = CS_VREDRAW | CS_HREDRAW;
	wndcls.lpfnWndProc = ::DefWindowProc;
	wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
	wndcls.hInstance = GetModuleHandle(NULL);
	wndcls.hIcon = ::LoadIcon(NULL, MAKEINTRESOURCE(IDR_MAINFRAME));
	wndcls.hCursor = ::LoadCursor(NULL, IDC_ARROW);
	wndcls.hbrBackground = NULL;
	wndcls.lpszMenuName = NULL;
	wndcls.lpszClassName = MAINCLASS_WINDOW;
	AfxRegisterClass(&wndcls);
	((CFrameWnd*)m_pMainWnd)->Create(MAINCLASS_WINDOW,  
		ILanguageManager::GetLanguageManager()->GetString(MAIN_FRAME_CAPTION_ID).c_str(), WS_VISIBLE, 
		CRect(CPoint(CW_USEDEFAULT, CW_USEDEFAULT), CSize(520, 545)));
	return TRUE;
}

std::vector<IComponent*> CApplication::GetComponents(){
	return m_aComponents;
}
void CApplication::Free(){
	delete this;
}
